import { ICat, IGetNav, INav, IHot, IGetHot } from "./interface";
import axios from "axios";
import qs from "qs";
const cat: ICat = {
  namespaced: true,
  state: {
    nav: [],
    data: [],
    loading: false,
  },
  actions: {
    async getnav({ commit }) {
      const res: INav[] = await axios("index.php?r=class/index&type=2").then(
        (re): INav[] => {
          return re.data.data;
        }
      );
      const action: IGetNav = {
        type: "GETNAV",
        payload: res,
      };
      commit(action);
    },
    async gethot({ commit }, { cid }) {
      const res: IHot[] = await axios
        .post(
          "/index.php?r=index/ajaxcat",
          qs.stringify({
            token: false,
            px: "hot",
            cid: cid,
            advVersion: 1,
            page: 1,
            cac_id: "",
          })
        )
        .then((res): IHot[] => {
          console.log(res.data.data.content);
          return res.data.data.content;
        });
      const action: IGetHot = {
        type: "GETHOT",
        payload: res,
      };
      commit(action);
    },
    async getsell({ commit }, { cid }) {
      const res: IHot[] = await axios
        .post(
          "/index.php?r=index/ajaxcat",
          qs.stringify({
            token: false,
            px: "sell",
            cid: cid,
            advVersion: 1,
            page: 1,
            cac_id: "",
          })
        )
        .then((res): IHot[] => {
          console.log(res.data.data.content);
          return res.data.data.content;
        });
      const action: IGetHot = {
        type: "GETSELL",
        payload: res,
      };
      commit(action);
    },
    async getlatest({ commit }, { cid }) {
      const res: IHot[] = await axios
        .post(
          "/index.php?r=index/ajaxcat",
          qs.stringify({
            token: false,
            px: "latest",
            cid: cid,
            advVersion: 1,
            page: 1,
            cac_id: "",
          })
        )
        .then((res): IHot[] => {
          console.log(res.data.data.content);
          return res.data.data.content;
        });
      const action: IGetHot = {
        type: "GETLATEST",
        payload: res,
      };
      commit(action);
    },
    async getprice({ commit }, { cid }) {
      const res: IHot[] = await axios
        .post(
          "/index.php?r=index/ajaxcat",
          qs.stringify({
            token: false,
            px: "price",
            cid: cid,
            advVersion: 1,
            page: 1,
            cac_id: "",
          })
        )
        .then((res): IHot[] => {
          console.log(res.data.data.content);
          return res.data.data.content;
        });
      const action: IGetHot = {
        type: "GETPRICE",
        payload: res,
      };
      commit(action);
    },
    setload({ commit }) {
      const action: { type: string } = {
        type: "SETLOAD",
      };
      commit(action);
    },
  },
  mutations: {
    GETNAV(state, action) {
      state.nav = action.payload;
    },
    GETHOT(state, action) {
      state.data = action.payload;
      state.loading = false;
    },
    GETSELL(state, action) {
      state.data = action.payload;
      state.loading = false;
    },
    GETPRICE(state, action) {
      state.data = action.payload;
      state.loading = false;
    },
    GETLATEST(state, action) {
      state.data = action.payload;
      state.loading = false;
    },
    SETLOAD(state) {
      state.loading = true;
    },
  },
};
export default cat;
